package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.qi.QiIndexMajor;
import com.ysd.lis.entity.qi.QiMajor;
import com.ysd.lis.entity.qi.QiMajorBusiness;
import com.ysd.lis.mapper.qi.QiIndexMajorMapper;
import com.ysd.lis.mapper.qi.QiMajorBusinessMapper;
import com.ysd.lis.mapper.qi.QiMajorMapper;
import com.ysd.lis.service.qi.QiMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-03-03
 */
@Service
public class QiMajorServiceImpl extends ServiceImpl<QiMajorMapper, QiMajor> implements QiMajorService {

    @Autowired
    private QiMajorMapper qiMajorMapper;

    @Autowired
    QiIndexMajorMapper qiIndexMajorMapper;
    @Autowired
    private QiMajorBusinessMapper qiMajorBusinessMapper;
    @Override
    public Result getQiMajorList() {
        LambdaQueryWrapper<QiMajor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QiMajor::getDelFlag, 0);
        lambdaQueryWrapper.orderByAsc(QiMajor::getMajorCode);
        List<QiMajor> list = qiMajorMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result addQiMajor(QiMajor qiMajor) {
        if (qiMajor.getId() == null){
            LambdaQueryWrapper<QiMajor> lambdaQueryWrapper = new LambdaQueryWrapper<QiMajor>().eq(QiMajor::getMajorCode, qiMajor.getMajorCode()).eq(QiMajor::getDelFlag,0);
            List<QiMajor> list = this.list(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(list)){
                return Result.fail("该专业编码已存在");
            }
        }

        boolean save = this.save(qiMajor);
        if (save) {
            return Result.succ(1, "添加成功", null);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result updateQiMajor(QiMajor qiMajor) {
        List<QiMajor> list = this.list(new LambdaQueryWrapper<QiMajor>().eq(QiMajor::getMajorCode, qiMajor.getMajorCode()).eq(QiMajor::getDelFlag,0));
        if (CollectionUtils.isNotEmpty(list)){
            for (QiMajor major : list) {
                if (!major.getId().equals(qiMajor.getId())){
                    return Result.fail("该专业编码已存在");
                }
            }
        }
        QiMajor majorById = this.getById(qiMajor.getId());
        if (!majorById.getMajorCode().equals(qiMajor.getMajorCode())){
            QiMajorBusiness qiMajorBusiness = new QiMajorBusiness();
            qiMajorBusiness.setMajorCode(qiMajor.getMajorCode());
            qiMajorBusinessMapper.update(qiMajorBusiness,new LambdaQueryWrapper<QiMajorBusiness>().eq(QiMajorBusiness::getMajorCode,majorById.getMajorCode()).eq(QiMajorBusiness::getDelFlag,0));
        }
        boolean save = this.updateById(qiMajor);
        if (save) {
            return Result.succ(1, "编辑成功", null);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result deleteQiMajor(QiMajor qiMajor) {
        LambdaQueryWrapper<QiIndexMajor>  lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(QiIndexMajor::getDelFlag, 0).eq(QiIndexMajor::getMajorCode, qiMajor.getMajorCode());
        Integer ii = qiIndexMajorMapper.selectCount(lambdaQueryWrapper1);
        Assert.isTrue(ii == 0, "该专业维护有绑定数据，禁止删除！");

        qiMajorBusinessMapper.delete(new LambdaQueryWrapper<QiMajorBusiness>().eq(QiMajorBusiness::getMajorCode, qiMajor.getMajorCode()));
        boolean delete = this.removeById(qiMajor.getId());
        if (delete) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }
    }
}
